<?php

// 管理员修改密码
// PUT manager.pwd.update

namespace app\api\resource\manager\pwd;
use think\Db;
use think\Loader;
use think\Session;

class Update
{
    // 方法
    public $_method = ['PUT'];
    // 前置
    public $_pre    = ['adminLogin'];
    // 描述
    public $_description = '管理员修改密码';
    // 参数
    public $_param  = [
        'passport' => '原密码',
        'password' => '密码',
        'cofimpwd' => '确认密码',
    ];
    // 返回
    public $_return = [
        201 => '修改成功',
        400 => '原密码不正确',
        422 => '请求参数不合法',
        500 => '服务器异常',
    ];

    public function run(&$request)
    {
        $params = $request->put();

        $validate = Loader::validate('Manager');
        if ( !$validate->scene('pwdUpdate')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $managerId = Session::get('manager.id');

        $where['id'] = $managerId;
        $where['password'] = md5($params['passport']);
        $manager = Db::name('manager')->where($where)->field('password',true)->find();

        if ( is_null($manager) ) return [400,'原密码不正确'];

        $data['password'] = md5($params['password']);
        $num = Db::name('manager')->where('id',$managerId)->update($data);

        return ($num >= 0) ? [ 201,[] ] : 500;
    }
}